package com.wpre.lotterysystem.service.Impl;

import com.wpre.lotterysystem.controller.param.CreatePrizeParam;
import com.wpre.lotterysystem.controller.param.PageParam;
import com.wpre.lotterysystem.dao.dataobject.PrizeDO;
import com.wpre.lotterysystem.dao.mapper.PrizeMapper;
import com.wpre.lotterysystem.service.PictureService;
import com.wpre.lotterysystem.service.PrizeService;
import com.wpre.lotterysystem.service.dto.PageListDTO;
import com.wpre.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-20
 * Time: 19:14
 */
@Service
public class PrizeServiceImpl implements PrizeService {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;

    //创建奖品
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        //1. 上传图片
        String fileName = pictureService.savePicture(picFile);

        //2. 将数据存储数据库到中
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(fileName);
        prizeDO.setPrice(param.getPrice());
        //存库
        prizeMapper.insert(prizeDO);
        return prizeDO.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        //总量
        int total = prizeMapper.count();

        //查询当前列表
        List<PrizeDTO> list = new ArrayList<>();
        List<PrizeDO> prizeDOList = prizeMapper.selectPrizeList(param.offset(), param.getPageSize());
        for (PrizeDO prizeDO : prizeDOList) {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());

            prizeDTO.setName(prizeDO.getName());

            prizeDTO.setImageUrl(prizeDO.getImageUrl());

            prizeDTO.setDescription(prizeDO.getDescription());

            prizeDTO.setPrice(prizeDO.getPrice());

            list.add(prizeDTO);
        }
        return new PageListDTO<>(total, list);
    }
}
